﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Timers;
//using System.Threading;

namespace SubTitleAddon
{
    public class SubtitleManager
    {
        public Action<Subtitle> Display;

        private List<Subtitle> mTitleList;
        //private DateTime mStartTime;
        //private DateTime mPauseTime;
        private TimeSpan mTotalTimeSpan;
        private TimeSpan mCurrentTimeSpan;
        private int mNextTitleIndex;
        private Timer mTimer;

        public double TotalMilliseconds
        {
            get { return mTotalTimeSpan.TotalMilliseconds; }
        }

        public SubtitleManager(Action<Subtitle> displayDelegate)
        {
            Display = displayDelegate;
            mTitleList = new List<Subtitle>();
            mTimer = new Timer();
            mTimer.Elapsed += new ElapsedEventHandler(mTimer_Elapsed);
        }

        private void RefreshTimer()
        {
          if (mNextTitleIndex >= 0 && mNextTitleIndex < mTitleList.Count)
          {
            mTimer.Interval = mTitleList[mNextTitleIndex].Start.TotalMilliseconds - mCurrentTimeSpan.TotalMilliseconds;
            mTimer.Enabled = true;
            mTimer.AutoReset = false;
            mTimer.Start();
          }
        }

        void mTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            if(Display != null && mNextTitleIndex >= 0)
            {
                Display(mTitleList[mNextTitleIndex]);
            }
            mNextTitleIndex++;
            RefreshTimer();
        }

        public void Read(string file)
        {
            using (StreamReader sr = new StreamReader(file, Encoding.Default))
            {
                string text = sr.ReadToEnd();
                Regex regex = new Regex(@"(\d+)\r\n(\d{2}:\d{2}:\d{2},\d{3}) --> (\d{2}:\d{2}:\d{2},\d{3})\r\n(.*?)\r\n\r\n", RegexOptions.Singleline);
                MatchCollection matches = regex.Matches(text);

                mTitleList.Clear();
                foreach (Match match in matches)
                {
                    GroupCollection groups = match.Groups;
                    var sub = new Subtitle
                    {
                        Id = int.Parse(groups[1].Value),
                        Start = TimeSpan.Parse(groups[2].Value.Replace(',', '.')),
                        Stop = TimeSpan.Parse(groups[3].Value.Replace(',', '.')),
                        Title = groups[4].Value
                    };
                    mTitleList.Add(sub);
                }
                if (mTitleList.Count > 0)
                {
                    mTotalTimeSpan = mTitleList[mTitleList.Count - 1].Stop;
                }
            }
        }

        public void Start()
        {
          Jump(new TimeSpan(0));
        }

        public void Stop()
        {
            //mTimer.Enabled = false;
        }

        //public void Pause()
        //{
        //    mPauseTime = DateTime.Now;
        //    mTimer.Enabled = false;
        //}

        //public void Resume()
        //{
        //    mStartTime = mStartTime + (DateTime.Now - mPauseTime);
        //    RefreshTimer();
        //}

        public void Jump(TimeSpan jumpTo)
        {
            mCurrentTimeSpan = jumpTo;

            mNextTitleIndex = mTitleList.FindIndex(sub=>sub.Start > mCurrentTimeSpan);
            RefreshTimer();
        }
    }
}
